package com.cn.longdistancebusstation.contacter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.enumxx.PassengerType;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Contacter;
import com.cn.longdistancebusstation.model.Result;
import com.cn.longdistancebusstation.util.RegularCheckUtil;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by mucheng on 2018/1/26.
 */

public class ContacterDetailActivity extends BaseActivity implements View.OnClickListener{
    private EditText mNameEditText, mCardNoEditText, mTypeEditText,mTelephoneEditText;
    private Contacter mContacter;
    private Button btn_conservation;
    private String mTypeFlag="0";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor_linkman);
        getTitleTextView().setText("修改联系人");
        this.showMenu1("",R.mipmap.ic_menu_delete);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        mContacter = (Contacter) intent.getSerializableExtra("contacter");
        //
        //
        mNameEditText =(EditText)findViewById(R.id.name_of_linkman);
        mNameEditText.setText(mContacter.getName());
        mCardNoEditText =(EditText)findViewById(R.id.idcard_numbers_of_linkman);
        mCardNoEditText.setText(mContacter.getCardNo());
        PassengerType passengerType = PassengerType.getPassengerType(mContacter.getType());
        mTypeEditText =(EditText)findViewById(R.id.type_of_linkman);
        mTypeEditText.setText("成人");
        mTelephoneEditText=(EditText)findViewById(R.id.telephone_number_of_linkman);
        mTelephoneEditText.setText(mContacter.getTelephone());
        mTelephoneEditText.setOnClickListener(this);
        //
        btn_conservation=(Button)findViewById(R.id.edit_conservation);
        btn_conservation.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.delete:
//                delete_linkman();
//                break;
            case R.id.edit_conservation:
                edit_conservation();
                break;
//            case R.id.type_of_linkman:
//                showPop();
//                break;

        }
    }

    private void showPop() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_linkman_type, null);
        final TextView tv_adult = (TextView) view.findViewById(R.id.tv_adult);
        final TextView tv_child = (TextView) view.findViewById(R.id.tv_child);
        final TextView tv_student = (TextView) view.findViewById(R.id.tv_student);
        final TextView tv_soldier = (TextView) view.findViewById(R.id.tv_soldier);
        TextView btnCancel = (TextView) view.findViewById(R.id.btn_cancel);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        popupWindow.setOutsideTouchable(true);
        View parent = LayoutInflater.from(this).inflate(R.layout.activity_add_new_passengers, null);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        //popupWindow在弹窗的时候背景半透明
        final WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.5f;
        getWindow().setAttributes(params);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                params.alpha = 1.0f;
                getWindow().setAttributes(params);
            }
        });
        tv_adult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeEditText.setText(tv_adult.getText().toString());
                mTypeFlag = "0";
                popupWindow.dismiss();
            }
        });
        tv_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeEditText.setText(tv_child.getText().toString());
                mTypeFlag = "1";
                popupWindow.dismiss();

            }
        });
        tv_student.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeEditText.setText(tv_student.getText().toString());
                mTypeFlag = "2";
                popupWindow.dismiss();

            }
        });
        tv_soldier.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeEditText.setText(tv_soldier.getText().toString());
                mTypeFlag = "3";
                popupWindow.dismiss();

            }
        });


        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    private void edit_conservation() {
        String strName = mNameEditText.getText().toString().trim();
        String strCardNo = mCardNoEditText.getText().toString().trim();
        String strType = mTypeEditText.getText().toString().trim();
        String strTelephone=mTelephoneEditText.getText().toString().trim();
        if (strName.equals("") || strCardNo.equals("") || strType.equals("")||strTelephone.equals("")) {
            Toast.makeText(ContacterDetailActivity.this, "姓名、身份证号,人员类型以及电话号码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!RegularCheckUtil.checkCardNo(strCardNo)) {
            Toast.makeText(ContacterDetailActivity.this, "请填写有效的证件号码", Toast.LENGTH_SHORT).show();
            return;
        }
        showHud();
        Retrofit retrofit = new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService = retrofit.create(HttpService.class);
        Call<Result> call = httpService.editContacter(mContacter.getId(), strName, strCardNo,strTelephone,mTypeFlag);
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                hideHud();
                Gson gson = new Gson();
                Result result = response.body();
                Boolean isSuccess = result.getHead().getSuccess();
                if (isSuccess) {
                    Toast.makeText(ContacterDetailActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    finish();

                } else {
                    Toast.makeText(ContacterDetailActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                hideHud();
                Toast.makeText(ContacterDetailActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void delete_linkman() {
        showHud();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<Result> call=httpService.deleteContacter(mContacter.getId());
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                hideHud();
                Gson gson=new Gson();
                Result result=response.body();
                Boolean isSuccess=result.getHead().getSuccess();
                if(isSuccess){
                    Toast.makeText(ContacterDetailActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    finish();
                }else{
                    Toast.makeText(ContacterDetailActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                hideHud();
                Toast.makeText(ContacterDetailActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_delete_passengers, menu);
//        return super.onCreateOptionsMenu(menu);
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        if(item.getItemId() == R.id.delete){
//            delete_linkman();
//            return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }
    @Override
    public void onMenu1Click() {
        super.onMenu1Click();
        delete_linkman();
    }

}
